package com.yunfeisoft.business.controller;

import com.yunfeisoft.base.model.User;
import com.yunfeisoft.base.service.inter.UserService;
import com.yunfeisoft.business.model.FamilySatisfactionQuestionnaire;
import com.yunfeisoft.business.model.Room;
import com.yunfeisoft.business.service.inter.FamilySatisfactionQuestionnaireService;
import com.yunfeisoft.business.service.inter.RoomService;
import com.yunfeisoft.common.base.BaseController;
import com.yunfeisoft.common.utils.Page;
import com.yunfeisoft.common.utils.Response;
import com.yunfeisoft.common.utils.ResponseUtils;
import com.yunfeisoft.common.utils.Validator;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * ClassName: FamilySatisfactionQuestionnaireController
 * Description: 家属满意度调查表Controller
 * Author: Jackie liu
 * Date: 2020-05-22
 */
@Controller
public class FamilySatisfactionQuestionnaireController extends BaseController {

    @Autowired
    private FamilySatisfactionQuestionnaireService familySatisfactionQuestionnaireService;

    @Autowired
    private UserService userService;

    @Autowired
    private RoomService roomService;

    /**
     * 添加家属满意度调查表
     *
     * @param record
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/familySatisfactionQuestionnaire/save", method = RequestMethod.POST)
    @ResponseBody
    public Response save(FamilySatisfactionQuestionnaire record, HttpServletRequest request, HttpServletResponse response) {
        Validator validator = new Validator();
        validator.required(request, "roomId", "房间号为空");
        validator.required(request, "satisfactoryJobSatisfaction", "院部工作满意度为空");
        validator.required(request, "satisfactoryJobStaffSatisfaction", "院办工作人员服务热情满意度为空");
        validator.required(request, "healthSatisfactionSatisfaction", "院内总体环境、卫生满意度为空");
        validator.required(request, "efficiencySatisfactionSatisfaction", "院办工作责任、效率满意度为空");
        validator.required(request, "doctorWorkAbilitySatisfaction", "医生工作能力的满意度为空");
        validator.required(request, "doctorServiceAttitudeSatisfaction", "医生服务态度的满意度为空");
        validator.required(request, "nurseWorkAbilitySatisfaction", "护士工作能力的满意度为空");
        validator.required(request, "nurseServiceAttitudeSatisfaction", "护士服务态度的满意度为空");
        validator.required(request, "nurseResolveAbilitySatisfaction", "护士解决问题能力的满意度为空");
        validator.required(request, "canteenWorkerServiceSatisfaction", "食堂工作人员的服务态度满意度为空");
        validator.required(request, "canteenHealthSatisfaction", "食堂及餐厅卫生状况满意度为空");
        validator.required(request, "safetySatisfaction", "安全满意度为空");
        validator.required(request, "doormanResponsibilitySatisfaction", "门卫责任满意度为空");
        validator.required(request, "securityJobSatisfaction", "安保工作满意度为空");
        validator.required(request, "marketingIntroducesSatisfaction", "市场部介绍满意度为空");
        validator.required(request, "marketingVisitSatisfaction", "市场部参观满意度为空");
        validator.required(request, "marketingServiceSatisfaction", "市场部服务满意度为空");
        validator.required(request, "mostSatisfactionPerson", "最满意的工作人员为空");
        validator.required(request, "mostDissatisfiedPerson", "最不满意工作人员为空");
        if (validator.isError()) {
            return ResponseUtils.warn(validator.getMessage());
        }
        familySatisfactionQuestionnaireService.save(record);
        return ResponseUtils.success("保存成功");
    }

    /**
     * 修改家属满意度调查表
     *
     * @param record
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/familySatisfactionQuestionnaire/modify", method = RequestMethod.POST)
    @ResponseBody
    public Response modify(FamilySatisfactionQuestionnaire record, HttpServletRequest request, HttpServletResponse response) {
        Validator validator = new Validator();
        validator.required(request, "id", "参数错误");
        validator.required(request, "roomId", "房间号为空");
        validator.required(request, "satisfactoryJobSatisfaction", "院部工作满意度为空");
        validator.required(request, "satisfactoryJobStaffSatisfaction", "院办工作人员服务热情满意度为空");
        validator.required(request, "healthSatisfactionSatisfaction", "院内总体环境、卫生满意度为空");
        validator.required(request, "efficiencySatisfactionSatisfaction", "院办工作责任、效率满意度为空");
        validator.required(request, "doctorWorkAbilitySatisfaction", "医生工作能力的满意度为空");
        validator.required(request, "doctorServiceAttitudeSatisfaction", "医生服务态度的满意度为空");
        validator.required(request, "nurseWorkAbilitySatisfaction", "护士工作能力的满意度为空");
        validator.required(request, "nurseServiceAttitudeSatisfaction", "护士服务态度的满意度为空");
        validator.required(request, "nurseResolveAbilitySatisfaction", "护士解决问题能力的满意度为空");
        validator.required(request, "canteenWorkerServiceSatisfaction", "食堂工作人员的服务态度满意度为空");
        validator.required(request, "canteenHealthSatisfaction", "食堂及餐厅卫生状况满意度为空");
        validator.required(request, "safetySatisfaction", "安全满意度为空");
        validator.required(request, "doormanResponsibilitySatisfaction", "门卫责任满意度为空");
        validator.required(request, "securityJobSatisfaction", "安保工作满意度为空");
        validator.required(request, "marketingIntroducesSatisfaction", "市场部介绍满意度为空");
        validator.required(request, "marketingVisitSatisfaction", "市场部参观满意度为空");
        validator.required(request, "marketingServiceSatisfaction", "市场部服务满意度为空");
        validator.required(request, "mostSatisfactionPerson", "最满意的工作人员为空");
        validator.required(request, "mostDissatisfiedPerson", "最不满意工作人员为空");
        if (validator.isError()) {
            return ResponseUtils.warn(validator.getMessage());
        }
        familySatisfactionQuestionnaireService.modify(record);
        return ResponseUtils.success("保存成功");
    }

    /**
     * 查询家属满意度调查表
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/familySatisfactionQuestionnaire/query", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Response query(HttpServletRequest request, HttpServletResponse response) {
        String id = ServletRequestUtils.getStringParameter(request, "id", null);
        if (StringUtils.isBlank(id)) {
            return ResponseUtils.warn("参数错误");
        }
        FamilySatisfactionQuestionnaire record = familySatisfactionQuestionnaireService.load(id);
        if  (!ObjectUtils.isEmpty(record)) {
            User user = userService.load(record.getCreateId());
            record.setCreateName(user.getName());
            Room room = roomService.load(record.getRoomId());
            record.setRoomNum(room.getRoomNum());
            record.setFloorNum(room.getFloorNum());
            // 查询最满意和最不满意的人
            User user1 = userService.load(record.getMostDissatisfiedPerson());
            User user2 = userService.load(record.getMostSatisfactionPerson());
            record.setMostDissatisfiedPersonName(user1.getName());
            record.setMostSatisfactionPersonName(user2.getName());
        }
        return ResponseUtils.success(record);
    }

    /**
     * 分页查询家属满意度调查表
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/familySatisfactionQuestionnaire/list", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Response list(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> params = new HashMap<String, Object>();
        initParams(params, request);
        Page<FamilySatisfactionQuestionnaire> page = familySatisfactionQuestionnaireService.queryPage(params);
        return ResponseUtils.success(page);
    }

    /**
     * 批量删除家属满意度调查表
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/familySatisfactionQuestionnaire/delete", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Response delete(HttpServletRequest request, HttpServletResponse response) {
        String ids = ServletRequestUtils.getStringParameter(request, "ids", null);
        if (StringUtils.isBlank(ids)) {
            return ResponseUtils.warn("参数错误");
        }
        familySatisfactionQuestionnaireService.remove(ids.split(","));
        return ResponseUtils.success("删除成功");
    }
}
